package com.css.resoft.p2pmonitor.test;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.hbase.thrift2.generated.TColumnValue;
import org.apache.hadoop.hbase.thrift2.generated.TGet;
import org.apache.hadoop.hbase.thrift2.generated.THBaseService;
import org.apache.hadoop.hbase.thrift2.generated.TPut;
import org.apache.hadoop.hbase.thrift2.generated.TResult;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

import com.css.resoft.p2pmonitor.util.DateUtil;



public class GetData {
	public final static String slave1host = "39.107.224.196";
	public final static String slave3host = "39.107.112.148";
	public final static int port = 9090;
//	public final static String table = "J0901_CONTRACT";
	
	public static void main(String[] args) throws Exception {
		long startTime = System.currentTimeMillis();
		getData();
		long endTime = System.currentTimeMillis();
		System.out.println(endTime-startTime);
		System.out.println("ok");
		
	}
	

	private static void getData() throws Exception {
		String table = "J0901_CONTRACT";
		
		String orgcode = "91440300068588146D";
		String datadate = "2018-03-11";
		String xh = "6";
		byte [] row = getRowKey(orgcode,datadate,xh);
		
		TTransport transport = new TSocket(slave3host,port);
		TProtocol protocol = new TBinaryProtocol(transport);
		THBaseService.Iface client = new THBaseService.Client(protocol);
		transport.open();
		
		TGet get = new TGet();
		get.setRow(row);
		
		TResult result = client.get(ByteBuffer.wrap(table.getBytes()), get);
		if(result.row == null){
			System.out.println("dont get the data  return ");
			return;
		}
		for(TColumnValue columnValue : result.getColumnValues()){
			System.out.print("qualifier = " + new String(columnValue.getQualifier()));
			System.out.println(" value = " + new String(columnValue.getValue())+" ");
		}
		transport.close();
	}

	
	
	private static void putData() throws Exception{
		String table = "carnon";
		String rowkey = "91440300311777140D2018-01-2515278";
		TTransport transport = new TSocket(slave3host,port);
		TProtocol protocol = new TBinaryProtocol(transport);
		THBaseService.Iface client = new THBaseService.Client(protocol);
		transport.open();
		
	    TPut put = new TPut();  
	    put.setRow("103".getBytes());  
	  
	    TColumnValue columnValue = new TColumnValue();  
	    columnValue.setFamily("article".getBytes());  
	    columnValue.setQualifier("title,".getBytes());  
	    columnValue.setValue("change thirft".getBytes());  
	    List<TColumnValue> columnValues = new ArrayList<TColumnValue>();  
	    columnValues.add(columnValue);  
	    put.setColumnValues(columnValues);  
	   
	    client.put(ByteBuffer.wrap(table.getBytes()), put); 
	    
	    transport.close();
	}
	
	
	private static byte[] getRowKey(String orgCode, String dataDate, String xh) {
		
		byte [] buffer = new byte[12];
		
		int days = DateUtil.convert2Day(dataDate, DateUtil.DATE_FORMAT);
		int invert_days = Integer.MAX_VALUE - days;
		
		Bytes.putInt(buffer, 0, orgCode.hashCode()); //p2p机构代码
		Bytes.putInt(buffer, 4, invert_days); 
		Bytes.putInt(buffer, 8, Integer.parseInt(xh)); 
//		Bytes.putInt(buffer, 12, xmht.hashCode());
				
		return buffer ;
	}
	
}
